package com.mdd.admin.service.log.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mdd.admin.service.log.IOrderRefundLogService;
import com.mdd.admin.service.system.ISystemAuthAdminService;
import com.mdd.admin.service.user.IUserService;
import com.mdd.admin.vo.log.OrderRefundLogDetailVo;
import com.mdd.common.entity.log.OrderRefundLog;
import com.mdd.common.entity.system.SystemAuthAdmin;
import com.mdd.common.enums.OperateEnum;
import com.mdd.common.enums.OrderRefundStatusEnum;
import com.mdd.common.mapper.log.OrderRefundLogMapper;
import com.mdd.common.utils.TimeUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单退款日志实现类
 */
@Service
public class OrderRefundLogServiceImpl extends ServiceImpl<OrderRefundLogMapper, OrderRefundLog> implements IOrderRefundLogService {

    @Resource
    OrderRefundLogMapper orderRefundLogMapper;

    @Resource
    private IUserService userService;

    @Resource
    private ISystemAuthAdminService systemAuthAdminService;

    /**
     * 订单退款日志详情
     *
     * @param refundId 主键参数
     * @return OrderRefundLog
     */
    @Override
    public List<OrderRefundLogDetailVo> detail(Long refundId) {
        LambdaQueryWrapper<OrderRefundLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderRefundLog::getRefundId, refundId);
        lambdaQueryWrapper.orderByDesc(OrderRefundLog::getId);
        List<OrderRefundLog> list = super.list(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        Map<Integer, String> map = OperateEnum.getMap();
        Map<Integer, String> refundStatusMap = OrderRefundStatusEnum.getMap();
        List<OrderRefundLogDetailVo> targetList = Lists.newArrayList();
        List<Long> adminIds = list.stream().map(OrderRefundLog::getOperatorId).filter(item -> item != null).distinct().collect(Collectors.toList());
        List<SystemAuthAdmin> adminList = systemAuthAdminService.listByAdminIds(adminIds);
        Map<Long, String> adminMap = Maps.newHashMap();
        if (CollectionUtils.isNotEmpty(adminList)) {
            adminMap = adminList.stream().collect(Collectors.toMap(item -> Long.valueOf(item.getId()), item -> item.getNickname(), (a, b) -> b));
        }
        for (OrderRefundLog refundLog : list) {
            OrderRefundLogDetailVo vo = new OrderRefundLogDetailVo();
            BeanUtils.copyProperties(refundLog, vo);
            vo.setTypeName(map.get(refundLog.getType()));
            vo.setRefundStatusName(refundStatusMap.get(refundLog.getRefundStatus()));
            vo.setOperatorName(adminMap.get(refundLog.getOperatorId()));
            vo.setCreateTime(TimeUtil.timestampToDate(refundLog.getCreateTime()));
            targetList.add(vo);
        }
        return targetList;
    }

    @Override
    public void saveEntity(OrderRefundLog refundLog) {
        super.save(refundLog);
    }

    @Override
    public OrderRefundLog getByWechatRefundId(String infoRefundId) {
        LambdaQueryWrapper<OrderRefundLog> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(OrderRefundLog::getWechatRefundId, infoRefundId);
        lambdaQueryWrapper.last("limit 1");
        return super.getOne(lambdaQueryWrapper);
    }

    @Override
    public void updateByEntityId(OrderRefundLog refundLog) {
        super.updateById(refundLog);
    }
}
